package com.able.concurrency.t1;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author abel.he
 * @data 2018/8/15 19:41
 * @describe AtomicXxx 原子操作类型。其中每个方法都是原子性操作。可以保证线程安全
 */
public class Test11 {

    AtomicInteger count = new AtomicInteger(0);

    void m1() {
        for (int i = 0; i < 10000; i++) {
            count.incrementAndGet();
        }
    }

    public static void main(String[] args) {
        final Test11 test11 = new Test11();
        List<Thread> list = new ArrayList<Thread>(10);
        for (int i = 0; i < 10; i++) {
            list.add(new Thread(new Runnable() {
                public void run() {
                    test11.m1();
                }
            }));
        }
        for (Thread thread :
                list) {
            thread.start();
        }
        for (Thread thread: list) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(test11.count);
    }
}
